﻿Imports sonicERPDAL
Imports sonicERPinfo
Imports System.Data.SqlTypes
Imports System.Globalization
Imports System.Threading


Partial Public Class UsuarioLista

    Inherits System.Web.UI.Page


    Dim usuario As New UsuarioInfo
    Dim dal As New UsuarioDAL
    Dim util As New Util

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


        If Not IsPostBack Then
            Dim paginavisivel As Boolean
            paginavisivel = util.validarpagina(3)
            If Not paginavisivel Then
                FormsAuthentication.SignOut()
                Response.Redirect(ResolveUrl("~/Login.aspx"))
            End If
            getusuario()
        End If
    End Sub



    Private Sub gvList_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gvList.PageIndexChanging
        e.Cancel = True
        gvList.PageIndex = e.NewPageIndex
        getusuario()
    End Sub

    Protected Sub gvList_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvList.RowCommand

    End Sub

    Public Sub getusuario()
        usuario.apelido = txtusuario.Text

        If txtusuario.Text = "" Then
            usuario.apelido = Convert.ToString(DBNull.Value)
        Else
            usuario.apelido = txtusuario.Text
        End If

        If txtdataininico.Text = "" Then
            Dim data As DateTime = DateTime.Today.AddMonths(-2)
            txtdataininico.Text = Convert.ToString(data.ToShortDateString)
            usuario.dtinicio = Convert.ToDateTime(txtdataininico.Text)
            txtdataininico.Text = FormataData(txtdataininico.Text)
        Else

            Dim culture As New System.Globalization.CultureInfo("pt-BR")
            Dim dt As String = Convert.ToDateTime(txtdataininico.Text).ToString("yyyy/MM/dd HH:mm:ss", culture)
            usuario.dtinicio = Convert.ToDateTime(dt).ToString("yyyy/MM/dd HH:mm:ss", culture)
        End If


        If txtdatafim.Text = "" Then
            Dim data As DateTime = DateTime.Today
            txtdatafim.Text = Convert.ToString(data.ToShortDateString)
            usuario.dt_fim = Convert.ToDateTime(txtdatafim.Text)
            txtdatafim.Text = FormataData(txtdatafim.Text)
        Else
            Dim culture As New System.Globalization.CultureInfo("pt-BR")
            Dim dt As String = Convert.ToDateTime(txtdatafim.Text).ToString("yyyy/MM/dd HH:mm:ss", culture)
            usuario.dt_fim = Convert.ToDateTime(dt).ToString("yyyy/MM/dd HH:mm:ss", culture)
        End If

        gvList.DataSource = dal.lista(usuario)
        gvList.DataBind()

    End Sub

    Public Function FormataData(ByVal Data)
        If Data <> "" Then
            Dim dt As DateTime = Data
            Data = dt.ToString("dd/MM/yyyy")

        End If
        Return Data
    End Function



    Public Function datainicio()
        Dim dia As String = "01"
        Dim hoje As String
        Dim data As DateTime = DateTime.Now
        Dim brasil As New CultureInfo("pt-BR")
        data = DateAdd(DateInterval.Month, -1, Now)
        Dim dataini As DateTime
        hoje = Convert.ToString(dia & "/" & data.Month & "/" & data.Year)
        dataini = Convert.ToDateTime(hoje)
        Return dataini.ToString(brasil)

    End Function

    Public Function datafim()
        Dim dia As Integer
        Dim hoje As String
        Dim data As DateTime = DateTime.Now
        Dim brasil As New CultureInfo("pt-BR")
        data = DateAdd(DateInterval.Month, 1, Now)
        Dim datafi As DateTime
        dia = DateTime.DaysInMonth(data.Year, data.Month)
        hoje = Convert.ToString(dia & "/" & data.Month & "/" & data.Year)
        datafi = Convert.ToDateTime(hoje)
        Return datafi.ToString(brasil)

    End Function

    Protected Sub btnok_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnok.Click
        Try
            pnl1.Visible = False

            If txtdataininico.Text = "" Or txtdatafim.Text = "" Then
                txtdataininico.BackColor = Drawing.Color.White
                txtdatafim.BackColor = Drawing.Color.White
                getusuario()
                lblerro.Text = ""
                Return
            Else

                Dim di As DateTime
                DateTime.TryParseExact(txtdataininico.Text, "dd/MM/yyyy", Nothing, Nothing, di)
                Dim df As DateTime
                DateTime.TryParseExact(txtdatafim.Text, "dd/MM/yyyy", Nothing, Nothing, df)


                If di = DateTime.MinValue Then
                    txtdataininico.Focus()
                    txtdataininico.BackColor = Drawing.Color.Aqua
                    txtdatafim.BackColor = Drawing.Color.White
                    Throw New Exception("data invalida digite a data no formato ex: dd/mm/yyyy")
                Else
                    txtdataininico.BackColor = Drawing.Color.White
                    If df = DateTime.MinValue Then
                        txtdatafim.Focus()
                        txtdatafim.BackColor = Drawing.Color.Aqua
                        txtdataininico.BackColor = Drawing.Color.White
                        Throw New Exception("data invalida digite a data no formato ex: dd/mm/yyyy")
                    Else
                        txtdatafim.BackColor = Drawing.Color.White
                    End If
                End If
            End If
            getusuario()
            lblerro.Text = ""
        Catch ex As Exception
            pnl1.Visible = True
            lblerro.Text = ex.Message
        End Try
    End Sub

    Protected Sub btnimprimir_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnimprimir.Click
        Dim javascript As String = "<script language ='javascript'>"
        javascript += " window.open('Imprimir.aspx?idrelatorio=2&nome=" + Server.HtmlDecode(txtusuario.Text) + "&dtinicio=" + Server.HtmlDecode(txtdataininico.Text) + "" +
      "&dtfim=" + Server.HtmlDecode(txtdatafim.Text) + " ', '', 'letf=0,top=0,width=900px,height=900px,toolbar=0,scrollbars=1,status=0');"
        javascript += " </script>"
        Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), "imprimir", javascript)
    End Sub
End Class